package com.vogue.supplymall.upload.controller;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import com.vogue.supplymall.common.domain.dto.ControllerResultInfo;
import com.vogue.supplymall.common.domain.dto.UploadInfo;
import com.vogue.supplymall.common.exception.RemoveUploadException;
import com.vogue.supplymall.upload.domain.bean.ConstantBean;
import com.vogue.supplymall.upload.domain.service.UploadService;

/**
 * 店铺证照上传处理
 *
 * @author 崔岳
 */
@RestController
public class LicenceUploadController extends BaseController {

    public static final Logger LOGGER_LICENCEUPLOADCONTROLLER = LoggerFactory.getLogger(LicenceUploadController.class);

    @Resource
    private UploadService uploadService;

    /**
     * 上传店铺证照
     *
     * @param appver appver
     * @param uploadInfo uploadInfo
     * @return ResponseEntity
     */
    @RequestMapping(value = "/is/{appver}/upload/licence", method = RequestMethod.POST)
    public ResponseEntity<Object> uploadLicence(@PathVariable("appver") String appver,
                                                @RequestBody UploadInfo uploadInfo) {
        //模块名字规则 entmanagement\licence\店铺id\证照类型
        uploadInfo.setModuleName(ConstantBean.LICENCE_UPLOAD_MODULE_NAME + "/" + uploadInfo.getModuleName());
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setData(uploadService.uploadResource(uploadInfo));
        resultInfo.setResult(true);
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }

    /**
     * Remove refund img response entity.
     *
     * @param appver             the appver
     * @param uploadResourceInfo the upload resource info
     * @return the response entity
     */
    @RequestMapping(value = "/is/{appver}/upload/obj/remove", method = RequestMethod.POST)
    public ResponseEntity<Object> removeObject(@PathVariable("appver") String appver,
                                               @RequestBody UploadInfo uploadResourceInfo) {
        LOGGER_LICENCEUPLOADCONTROLLER.info("remove object start >>>>>> " + uploadResourceInfo.getDeletePath());
        if (StringUtils.isEmpty(uploadResourceInfo.getDeletePath())) {
            throw new RemoveUploadException("");
        }
        ControllerResultInfo resultInfo = new ControllerResultInfo();
        resultInfo.setResult(true);
        resultInfo.setData(uploadService.removeResource(uploadResourceInfo));
        LOGGER_LICENCEUPLOADCONTROLLER.info(" <<<<<< remove object end");
        return new ResponseEntity<>(resultInfo, HttpStatus.OK);
    }
}
